package com.example.myandroidactualcombat;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.example.myandroidactualcombat.data.Book;
import com.example.myandroidactualcombat.data.User;
import com.example.myandroidactualcombat.ui.dashboard.DashboardFragment;

import org.litepal.LitePal;

import java.util.List;

public class MainActivity extends AppCompatActivity{

    private EditText ed_user;
    private EditText ed_password;
    private Button bu_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        addUser();
        addListener();
        verifyStoragePermissions();
    }

    //张峰华！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
    private void initView() {
        //初始化控件
        ed_user = (EditText) findViewById(R.id.ed_user);
        ed_password = (EditText) findViewById(R.id.ed_password);
        bu_login = (Button) findViewById(R.id.bu_login);
    }

    private void addUser() {
        //向数据库增加数据
        User user1=new User(0,"张三","123");
        user1.save();
        User user2=new User(1,"张四","456");
        user2.save();
        User user3=new User(2,"张五","789");
        user3.save();
    }
    private  boolean login(){
        //设置一个变量为k初始值为false
        boolean k=false;
        //获取两个输入框里面的内容
        String ed_user1=ed_user.getText().toString().trim();
        String ed_password1=ed_password.getText().toString().trim();
        //把litepal里面的数据放在userList集合里面
        List<User>userList=LitePal.findAll(User.class);
        //循环对比litepal里面的所有条目
        for (int i=0;i<userList.size();i++){
            //获取数据
            User user=userList.get(i);
            //对比输入框里面的内容是否和litepal一致
            if (ed_user1.equals(user.getUsername())&&ed_password1.equals(user.getPassword())){
                //如果一致k为true
                    k=true;
                SharedPreferences.Editor editor= PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit();
                editor.putString("username",user.getUsername());
                editor.putInt("userid",user.getId());
                editor.apply();
            }
        }
        return k;
    }
    private void addListener() {
        //按钮的点击事件
        bu_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //当输入框里面的内容和litepal的某个条目一致时，登录成功，并且跳转到下一个活动
                if (login()==true){
                    startActivity(new Intent(MainActivity.this, UserActivity.class));
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    Log.d("MainActivity", "onClick");
                }else{
                    Toast.makeText(MainActivity.this, "账号不存在", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };

    public void verifyStoragePermissions() {
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(MainActivity.this,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(MainActivity.this,
                        PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
